Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.default = void 0);
var e = i(require("../@babel/runtime/regenerator")),
  t = require("../@babel/runtime/helpers/asyncToGenerator"),
  r = require("../@babel/runtime/helpers/objectSpread2"),
  n = require("../@babel/runtime/helpers/objectWithoutProperties"),
  u = require("../@babel/runtime/helpers/classCallCheck"),
  a = require("../@babel/runtime/helpers/createClass"),
  o = i(require("md5")),
  s = ["success", "fail"];
function i(e) {
  return e && e.__esModule ? e : { default: e };
}
var c = wx.getRealtimeLogManager(),
  l = (function () {
    return a(
      function e() {
        u(this, e);
      },
      [
        {
          key: "request",
          value: function (e) {
            return new Promise(function (t, u) {
              var a = e.success,
                o =
                  void 0 === a
                    ? function (e) {
                        t(e.data);
                      }
                    : a,
                i = e.fail,
                c = void 0 === i ? u : i,
                l = n(e, s);
              wx.request(r({ success: o, fail: c }, l));
            });
          },
        },
        {
          key: "randomString",
          value: function (e) {
            for (
              var t = e || 32,
                r = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
                n = r.length,
                u = "",
                a = 0;
              a < t;
              a++
            )
              u += r.charAt(Math.floor(Math.random() * n));
            return u;
          },
        },
        {
          key: "getHeader",
          value: function () {
            var e = Math.round(new Date().getTime() / 1e3),
              t = this.randomString(16);
            return {
              app: "san",
              ts: e,
              nonce: t,
              sign: (0, o.default)(
                "".concat("san").concat(e).concat("Uw4ohXWATiJOfrgT").concat(t),
              ),
            };
          },
        },
        {
          key: "doRequest",
          value:
            ((i = t(
              e.default.mark(function t(n, u) {
                var a,
                  o,
                  s,
                  i = arguments;
                return e.default.wrap(
                  function (e) {
                    for (;;)
                      switch ((e.prev = e.next)) {
                        case 0:
                          return (
                            (a =
                              i.length > 2 && void 0 !== i[2] ? i[2] : "POST"),
                            (o = i.length > 3 && void 0 !== i[3] ? i[3] : {}),
                            (e.next = 4),
                            this.getNetwork()
                          );
                        case 4:
                          return (
                            "none" === e.sent && console.log("网络异常"),
                            (e.next = 8),
                            this.request({
                              url: n,
                              data: u,
                              method: a,
                              header: r(r({}, this.getHeader()), o),
                              timeout: 6e5,
                            })
                          );
                        case 8:
                          return (
                            200 !== (s = e.sent).code &&
                              0 !== s.code &&
                              c.info({ url: n, param: u, response: s }),
                            e.abrupt("return", s)
                          );
                        case 11:
                        case "end":
                          return e.stop();
                      }
                  },
                  t,
                  this,
                );
              }),
            )),
            function (e, t) {
              return i.apply(this, arguments);
            }),
        },
        {
          key: "post",
          value: function (e, t, r) {
            return this.doRequest(e, t, "POST", r);
          },
        },
        {
          key: "get",
          value: function (e, t, r) {
            return this.doRequest(e, t, "GET", r);
          },
        },
        {
          key: "delete",
          value: function (e, t, r) {
            return this.doRequest(e, t, "DELETE", r);
          },
        },
        {
          key: "put",
          value: function (e, t, r) {
            return this.doRequest(e, t, "PUT", r);
          },
        },
        {
          key: "getNetwork",
          value: function () {
            return new Promise(function (e) {
              wx.getNetworkType({
                success: function (t) {
                  e(t.networkType);
                },
              });
            });
          },
        },
      ],
    );
    var i;
  })();
exports.default = l;
